﻿//------------------------------------------------
// StackTenButtons.cs (c) 2006 by Charles Petzold
//------------------------------------------------
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace Petzold.StackTenButtons
{
    class StackTenButtons : Window
    {
        //[STAThread]
        //public static void Main()
        //{
        //    Application app = new Application();
        //    app.Run(new StackTenButtons());
        //}
        public StackTenButtons()
        {
            Title = "Stack Ten Buttons";

            StackPanel stack = new StackPanel();
            // stack.Background = Brushes.Aquamarine;
            stack.Orientation = Orientation.Vertical;
            Random rand = new Random();

            for (int i = 0; i < 10; i++)
            {
                Button btn = new Button();
                btn.HorizontalAlignment = HorizontalAlignment.Center;

                btn.Margin = new Thickness(5);
                btn.Name = ((char)('A' + i)).ToString();
                btn.FontSize += rand.Next(30);
                btn.Content = "Button " + btn.Name + " says 'Click me'";

                stack.Children.Add(btn);
            }
            stack.AddHandler(Button.ClickEvent, new RoutedEventHandler(ButtonOnClick));

            stack.HorizontalAlignment = HorizontalAlignment.Center;
            stack.Margin = new Thickness(5);


            ScrollViewer scrollV = new ScrollViewer();
            scrollV.Content = stack;
            scrollV.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
            scrollV.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
           // SizeToContent = System.Windows.SizeToContent.Width;
            Content = scrollV;
           // Content = stack;

            //Viewbox vb = new Viewbox();
            //vb.Child = stack;
            //Content = vb;

            Console.WriteLine(stack.FindName("A") + "sss");
        }

        void ButtonOnClick(object sender,
 RoutedEventArgs args)
        {
            Button btn = args.Source as Button;

            MessageBox.Show("Button " + btn.Name +
 " has been clicked",
                            "Button Click");
        }
    }
}
